/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("yg_jsgf_head",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.MyTable.IServices;
using System.Collections;
using VOL.Core.DBManager;
using System.Linq;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels.eager;

namespace VOL.MyTable.Controllers
{
    public partial class yg_jsgf_headController
    {
        private readonly Iyg_jsgf_headService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        
        [ActivatorUtilitiesConstructor]
        public yg_jsgf_headController(
            Iyg_jsgf_headService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        [HttpPost, Route("get_jsgf_item")]//获取技术模版
        public dynamic get_jsgf_item(string MOBBH)
        {
            Hashtable rtn_hst = new Hashtable();
            var all_yg_jsgf_item = DBServerProvider.DbContext.Set<yg_jsgf_item>().Where(w => w.MOBBH == MOBBH).ToList();//.Select((pname, luxs_project_id)=>new {pname,luxs_project_id});
            rtn_hst.Add("all_yg_jsgf_item", all_yg_jsgf_item);
            var yg_xx_list = DBServerProvider.DbContext.Set<yg_xx>().Where(w => !string.IsNullOrEmpty(w.SHUOM_1)).ToList();//亿格配置变量
            rtn_hst.Add("yg_xx_list", yg_xx_list);
            return rtn_hst;      
        }
        [HttpPost, Route("get_dms_market")]//获取DMS市场选项
        public dynamic get_dms_market(string xiangm)
        {
            Hashtable rtn_hst = new Hashtable();
            string sqlstr1 = "select * from TYCP204 where xiangm='"+ xiangm + "'";
            List<dynamic> dms_market = Other_Oracle_Helper.query_common(sqlstr1, "yige");
            rtn_hst.Add("dms_market", dms_market);
            var yg_xx_list = DBServerProvider.DbContext.Set<yg_xx>().Where(w => !string.IsNullOrEmpty(w.SHUOM_1)).ToList();//亿格配置变量
            rtn_hst.Add("yg_xx_list", yg_xx_list);
            return rtn_hst;
        }
        [HttpPost, Route("get_yg_custom_bom")]//获取亿格客户BOM
        public dynamic get_yg_custom_bom(string xiangm)
        {
            
            string sqlstr = "select * from TYCP206,Tyjc061 where TYCP206.wuliao=Tyjc061.WULIAO and  TYCP206.xiangm='" + xiangm + "' order by TYCP206.hangh";
            //Console.WriteLine(sqlstr);
            Hashtable rtn_hst = new Hashtable();
            //var yg_custom_bom = Other_Oracle_Helper.query_common(sqlstr, "118");//用于调试
            List<dynamic> yg_custom_bom = Other_Oracle_Helper.query_common(sqlstr, "yige");
           // List<string> wul_list = yg_custom_bom.Select(s => s.WULIAO).ToList();

            //string t = string.Join("','", wul_list);
            //t = " in ('" + t + "')";//组合SQL语句in('a','b')
            //Console.WriteLine(t);
            //List<Tyjc061> Tyjc061_list = Other_Oracle_Helper.query<Tyjc061>("select * from Tyjc061 where WULIAO " + t, conn_yg);//物料主数据
            rtn_hst.Add("yg_custom_bom", yg_custom_bom);
            return rtn_hst;
        }
        //动态解析亿格表达式
        [HttpPost, Route("jx_express")]
        public WebResponseContent jx_express(string xiangm,string zhuwul)
        {
            WebResponseContent webResponse = new WebResponseContent();
            List<yg_xx> yg_xx_list = DBServerProvider.DbContext.Set<yg_xx>().ToList();
            //string xiangm = "23100056";
            //string zhuwl = "U6T4BKH044";
            //string conn_yg = "118";//yige用于调试
            string conn_yg = "yige";//yige
            //List<yg_custom_bom> yg_custom_bom_list = new List<yg_custom_bom>();//自定义表-生成亿格客户BOM
            DBServerProvider.DbContext.RemoveRange(DBServerProvider.DbContext.Set<yg_custom_bom>().Where(a => a.XIANGM == xiangm));
            string dmsxx_sql = "select * from TYCP204 where xiangm='" + xiangm + "'";//DMS传给亿格的订单选项
            List<Tycp107> ybbom_list = new List<Tycp107>();
            get_all_ybbom_list(Other_Oracle_Helper.query<Tycp107>("select * from tycp107", conn_yg), zhuwul, ybbom_list);
            var dmsxx_list = Other_Oracle_Helper.query<Tycp204>(dmsxx_sql, conn_yg);
            for (int i = 0; i < ybbom_list.Count; i++)
            {
                yg_custom_bom ycb = new yg_custom_bom();
                string rtn_bds = "";
                if (ybbom_list[i].BIAODS == "/" || jx_single_express(ybbom_list[i].BIAODS, dmsxx_list, yg_xx_list, ref rtn_bds))
                {
                    ycb.XIANGM = xiangm;
                    ycb.KEHWL = zhuwul;//父物料
                    ycb.SHUL = ybbom_list[i].JINGSL;
                    ycb.GONGW = ybbom_list[i].GONGW;
                    ycb.HANGH = ybbom_list[i].HANGH;
                    ycb.WULIAO = ybbom_list[i].WULIAO;//子物料
                    ycb.rtn_bds = rtn_bds;
                    DBServerProvider.DbContext.Add(ycb);
                }

            }
            var rtn = ybbom_list;
            DBServerProvider.DbContext.SaveChanges();
            return webResponse.OK("完成BOM解析，请查看客户BOM！");
        }
        /// <summary>
        /// 前端获取样板BOM和物料
        /// </summary>
        /// <param name="touwul">头物料号</param>
        /// <returns></returns>
       [HttpPost, Route("get_yangb_bom")]
        public dynamic  get_yangb_bom(string touwul) 
        {
            Hashtable rtn_hst = new Hashtable();
            List<Tycp107> ybbom_list = new List<Tycp107>();
            string conn_yg = "yige";//yige
            get_all_ybbom_list(Other_Oracle_Helper.query<Tycp107>("select * from tycp107", conn_yg), touwul, ybbom_list);//BOM数据
            List<string> wul_list = ybbom_list.Select(s => s.WULIAO).ToList();
          
            string t = string.Join("','", wul_list);
            t = " in ('" + t + "')";//组合SQL语句in('a','b')
            //Console.WriteLine(t);
            List<Tyjc061> Tyjc061_list = Other_Oracle_Helper.query<Tyjc061>("select * from Tyjc061 where WULIAO "+t, conn_yg);//物料主数据

            var rtn_ybbom_tree = ybbom_list.Select(s => new//传给前端3个字段（id子节点，parentId父节点，label子节点说明），用于前端生成tree
            {
                id = s.WULIAO,//
                parentId = s.ZHUWL,
                label = s.WULIAO.ToString() + "[" + Tyjc061_list.Where(w => w.WULIAO == s.WULIAO).Select(s => s.SHUOM).FirstOrDefault() + "]"//组合说明
            });
            rtn_hst.Add("ybbom_list", rtn_ybbom_tree);
            rtn_hst.Add("all_ybbom_list", ybbom_list);
            rtn_hst.Add("wu_list", Tyjc061_list);
            return rtn_hst;
        }
        /// <summary>
        /// 递归获取样板BOM
        /// </summary>
        /// <param name="all_Tycp107_list"></param>
        /// <param name="touwul">头物料号</param>
        /// <param name="return_all_children"></param>
        /// <returns></returns>
        private List<Tycp107> get_all_ybbom_list(List<Tycp107> all_Tycp107_list,string touwul, List<Tycp107> return_all_children)
        {
            List<Tycp107> child = all_Tycp107_list.Where(w => w.ZHUWL == touwul).ToList();
            if (child.Count > 0)
            {
                foreach (var item in child)
                {
                    return_all_children.Add(item);
                    get_all_ybbom_list(all_Tycp107_list, item.WULIAO, return_all_children);
                }
            }
            return return_all_children;
        }


        bool jx_single_express(string biaods, List<Tycp204> dmsxx_list, List<yg_xx> yg_xx_list, ref string rtn_bds)
        {
            //bool rtn = false;
            rtn_bds = "";
            string biaods_t = biaods;
            if (!string.IsNullOrEmpty(biaods) && biaods != "/")
            {

                // biaods_t = biaods_t
                for (int i = 0; i < dmsxx_list.Count; i++)
                {
                    biaods_t = biaods_t.Replace(dmsxx_list[i].TEZM, dmsxx_list[i].TEZXJ);
                }
                for (int i = 0; i < yg_xx_list.Count; i++)
                {
                    biaods_t = biaods_t.Replace(yg_xx_list[i].XUANX, "'" + yg_xx_list[i].XUANX + "'");
                }
                biaods_t = biaods_t.Replace("{", "(").Replace("}", ")");
                rtn_bds = biaods_t;
                string pd_sql = "select * from Sys_User where " + biaods_t;
                List<dynamic> rnt_list = DBServerProvider.SqlDapper.QueryList<dynamic>(pd_sql, "").ToList();
                if (rnt_list.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
                // Console.WriteLine(biaods_t);
            }
            else
            {
                return true;
            }

            //return rtn;
        }

    }
}
